<?php
/**
 * Created by PHPStorm
 * User: xiest
 * Date: 2021/6/8
 * Email: <939369942@qq.com>
 */
namespace app\controller\api;

use app\BaseController;
use app\common\repositories\info\OrderRepository;
use app\common\repositories\system\SystemGroupRepository;
use app\common\repositories\user\UserOperationRepository;
use app\common\repositories\work\WorkPostRepository;
use basic\services\LocationService;
use basic\services\MiniProgramService;
use basic\services\UploadService;
use basic\services\WechatService;
use think\App;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Log;

class Common extends BaseController{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * upload tempKey
     * @return mixed
     * User: xiest
     * Date: 2022/3/21
     */
    public function tempKey()
    {
        $upload = UploadService::create();
        return success($upload->getKeys());
    }

    public function upload($field)
    {
        $upload = UploadService::create();
//        $orderSn = input('order_sn','');
//        if(!$orderSn)
//            return fail('订单不存在');
//
//        if(!app()->make(OrderRepository::class)->getWhere(['order_sn'=>$orderSn]))
//            return fail('订单不存在');

        $uploadInfo = $upload->toPath('upload/')->move($field);

        if($uploadInfo == false)
            return fail($upload->getError());
        return success(['path'=>env('SITE_URL').$upload->getUploadInfo()['dir']]);
    }


    public function stream()
    {
        $content =$this->request->param('file','');
        if(!$content) throw new ValidateException('文件内容不能为空');
        $ext =$this->request->param('ext','');
        $upload = UploadService::create();
        $uploadInfo = $upload->toPath(date('Ymd'))->stream($content,$ext);
        if($uploadInfo == false)
            return fail($upload->getError());
        return success(['path'=>$upload->getUploadInfo()['dir']]);
    }


    /**
     * 微信小程序支付回调
     * @return \think\Response|void
     * User: xiest
     * Date: 2022/3/21
     */
    public function MiniProgramNotice()
    {
        try{
            return response(MiniProgramService::create()->handleNotify()->getContent());
        }catch (\Exception $e){
            Log::info('小程序支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
        }
    }

    /**
     * 微信支付回调
     * @return \think\Response|void
     * User: xiest
     * Date: 2022/3/21
     */
    public function WechatNotice()
    {
        try{
            return response(WechatService::create()->handleNotify()->getContent());
        }catch (\Exception $e){
            Log::info('微信支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
        }
    }


    public function WechatServe()
    {
        return WechatService::create()->serve();
    }


    /**
     * JS-config
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * User: xiest
     * Date: 2022/3/21
     */
    public function jsConfig()
    {
        $apiList = ['updateTimelineShareData', 'updateAppMessageShareData','chooseWXPay'];
        $data = WechatService::create()->getService()->jssdk->buildConfig($apiList, false, true,false,[],input('url'));
        return success($data);
    }


    /**
     * 获取图片base64
     * @return mixed
     */
    public function get_image_base64()
    {
        [$imageUrl, $codeUrl] = [input('image'),input('code')];
        try {
            $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : '';
            if (!$codeTmp) {
                $putCodeUrl = put_image($codeUrl);
                $code = $putCodeUrl ? image_to_base64('./runtime/temp' . $putCodeUrl) : '';
                $code && unlink('./runtime/temp' . $putCodeUrl);
            }

            $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : '';
            if (!$imageTmp) {
                $putImageUrl = put_image($imageUrl);
                $image = $putImageUrl ? image_to_base64('./runtime/temp' . $putImageUrl) : '';
                $image && unlink('./runtime/temp' . $putImageUrl);
            }
            return app('json')->success(compact('code', 'image'));
        } catch (\Exception $e) {
            return app('json')->fail($e->getMessage().$e->getFile().$e->getLine());
        }
    }
}